/**
 *Copyright [2010] [lcj(caijunlu@gmail.com)]
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License. 
 *You may obtain a copy of the License at 
 *             http://www.apache.org/licenses/LICENSE-2.0 
 *Unless required by applicable law or agreed to in writing, 
 *software distributed under the License is distributed on an "AS IS" BASIS, 
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 *either express or implied. See the License for the specific language governing permissions and limitations under the License
 */
package org.independent.servlet.session.interceptor;




import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.independent.servlet.session.manager.SessionsManager;

/**
 * intercept the method of HttpServletResponse's addCookie, before the
 * httpServletResponse return to the client ,if the the client could use the
 * Cookie
 * 
 * @see javax.servlet.http.HttpServletResponse.addCookie()
 * @invoke by
 *         org.independent.servlet.session.interceptor.ServletServiceInterceptor
 * 
 * @author lcj
 * 
 */
public aspect ResponseCookieInterceptor {

	pointcut interceptAddCookie(
			HttpServletResponse httpServletResponse,
			Cookie cookie):(execution(public void javax.servlet.http.HttpServletResponse.addCookie(Cookie))||call(public void javax.servlet.http.HttpServletResponse.addCookie(Cookie)))&&target(httpServletResponse)&&args(cookie);

	void around(HttpServletResponse httpServletResponse,
			Cookie cookie)   :interceptAddCookie(httpServletResponse, cookie){
		Cookie newCookie=new Cookie("JSessionId","");
		if (cookie.getName().equalsIgnoreCase(
				SessionsManager.sessionIdName)) {
			newCookie.setValue(SessionsManager.getInstance().formatSessionId(
					cookie.getValue()));
			proceed(httpServletResponse, newCookie);
		}
	}

}